package com.igeek.web;

import com.igeek.domain.Cart;
import com.igeek.domain.CartItem;
import com.igeek.domain.Page;
import com.igeek.domain.Product;
import com.igeek.service.ProductService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@WebServlet("/product")
public class ProductServlet extends BaseServlet {

    public void index(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        ProductService productService=new ProductService();
        List<Product> hotProductList=productService.getHotproduct();
        List<Product> newProductList=productService.getNewproduct();
        request.setAttribute("hotProductList",hotProductList);
        request.setAttribute("newProductList",newProductList);
        request.getRequestDispatcher("/index.jsp").forward(request,response);
    }

    public void  findProductByCid(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String cid=request.getParameter("cid");
        String currentPage=request.getParameter("currentPage");
        int current;
        if(currentPage==null){
            current=1;
        }else{
            current=Integer.parseInt(currentPage);
        }
        //调用业务逻辑层
        ProductService productService=new ProductService();
        Page page=productService.getProductByCid(cid,current);

        //取出浏览记录
//        Product historyProduct=null;
        List<Product> historyProductList=new ArrayList<>();
        Cookie[] cookies=request.getCookies();
        if(cookies!=null){
            for(Cookie cookie:cookies){
                if("pids".equals(cookie.getName())){
                    //1,2,3,4,5
                    //5,4,3,2,1
                    String pids=cookie.getValue();
                    String [] pidArr=pids.split(",");
                    //超出7条数据的处理
                    int length;
                    if(pidArr.length<7){
                        length=0;
                    }else{
                        length=pidArr.length-7;
                    }
                    for(int i=pidArr.length-1;i>=length;i--){
                        Product p=productService.getProductByPid(pidArr[i]);
                        historyProductList.add(p);
                    }
//                    historyProduct=productService.getProductByPid(cookie.getValue());
                    break;
                }
            }
        }
        request.setAttribute("page",page);
        request.setAttribute("cid",cid);
        request.setAttribute("historyProductList",historyProductList);
        request.getRequestDispatcher("/product_list.jsp").forward(request,response);
    }

    public void findProductByPid(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String pid=request.getParameter("pid");
        String currentPage=request.getParameter("currentPage");
        String cid=request.getParameter("cid");
        ProductService productService=new ProductService();
        Product product=productService.getProductByPid(pid);
        request.setAttribute("product",product);
        request.setAttribute("currentPage",currentPage);
        request.setAttribute("cid",cid);
        //存历史浏览记录,所有的商品的pid
        //"cznnit,wunnit,whnnit,njnnit"
        String pids=pid;
        //先取,1,2,3,14
        //新的14
        //另外一种新的2
        //1,3,14,2
        Cookie [] cookies = request.getCookies();
        if(cookies!=null){
            for (Cookie cookie:cookies){
                if("pids".equals(cookie.getName())){
                    pids=cookie.getValue();
                    //判断新查看的商品是否已存在
                    if(pids.indexOf(pid)!=-1){
                        pids=pids.replace(","+pid,"");
                    }
                    pids+=","+pid;
                }
            }
        }
        Cookie cookie=new Cookie("pids",pids);
        cookie.setPath("/");
        response.addCookie(cookie);

        request.getRequestDispatcher("/product_info.jsp").forward(request,response);
    }

    /**
     * 把商品添加到购物车
     */
    public void addCart(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String pid=request.getParameter("pid");
        int buyCount=Integer.parseInt(request.getParameter("count"));
        HttpSession session=request.getSession();
        //先从session中取出购物车信息
        Cart cart=(Cart) session.getAttribute("cart");
        if(cart==null){
            cart=new Cart();
        }
        //构建购物项对象
        //vivo -- 1 -- 2298
        //vivo -- 3 -- 6894
        CartItem cartItem=new CartItem();
        ProductService productService=new ProductService();
        Product product=productService.getProductByPid(pid);
        cartItem.setProduct(product);
        //新增的小计
        double subTotal=product.getShop_price()*buyCount;
        //把购物项添加到集合中
        Map<String,CartItem> map=cart.getMap();
        double newSubTotal=subTotal;

        //查找一下新添加的商品是否已经在购物车中存在
        if(map.containsKey(pid)){
            //该购物车中已经存在该商品
            //修改购物项的购买的数量
            buyCount=map.get(pid).getCount()+buyCount;
            //修改小计:原来的商品的小计+新增的小计 2298+6894
            newSubTotal+=map.get(pid).getSubTotal();
        }
        cartItem.setCount(buyCount);
        cartItem.setSubTotal(newSubTotal);
        map.put(pid,cartItem);
        //然后把购物项集合存放到购物车中
        cart.setMap(map);
        cart.setTotal(cart.getTotal()+subTotal);
        //存放在session中的
        session.setAttribute("cart",cart);
        //带到页面展示
        request.getRequestDispatcher("/cart.jsp").forward(request,response);
    }
}
